package io.kiki.sba.registry.server.clientInterface.converter.pb;

import io.kiki.sba.registry.common.model.client.pb.ResultPb;
import io.kiki.sba.registry.common.model.client.pb.SyncConfigResponsePb;
import io.kiki.sba.registry.core.model.SyncConfigResponse;


public final class SyncConfigResponseConvertor {
    private SyncConfigResponseConvertor() {
    }

    public static SyncConfigResponse convert2Java(SyncConfigResponsePb syncConfigResponsePb) {

        if (syncConfigResponsePb == null) {
            return null;
        }

        SyncConfigResponse syncConfigResponse = new SyncConfigResponse();

        syncConfigResponse.setAvailableSegments(ListStringConvertor.convert2Java(syncConfigResponsePb.getAvailableSegmentsList()));

        syncConfigResponse.setRetryInterval(syncConfigResponsePb.getRetryInterval());
        syncConfigResponse.setMessage(syncConfigResponsePb.getResult().getMessage());
        syncConfigResponse.setSuccess(syncConfigResponsePb.getResult().getSuccess());

        return syncConfigResponse;
    }

    public static SyncConfigResponsePb convert2Pb(SyncConfigResponse syncConfigResponseJava) {

        if (syncConfigResponseJava == null) {
            return null;
        }

        SyncConfigResponsePb.Builder builder = SyncConfigResponsePb.newBuilder();
        builder.setRetryInterval(syncConfigResponseJava.getRetryInterval());
        builder.addAllAvailableSegments(syncConfigResponseJava.getAvailableSegments());

        ResultPb.Builder resultPbBuilder = ResultPb.newBuilder();
        if (syncConfigResponseJava.getMessage() != null) {
            resultPbBuilder.setMessage(syncConfigResponseJava.getMessage());
        }
        resultPbBuilder.setSuccess(syncConfigResponseJava.isSuccess());

        builder.setResult(resultPbBuilder.build());

        return builder.build();
    }
}
